package com.jy.rhin.application.cda;

import com.jy.api.hospital.HospitalAPI;
import com.jy.api.hospital.dto.HospitalDTO;
import com.jy.api.misc.dto.DaiMaDto;
import com.jy.api.misc.dto.ZhiYuMXCXDto;
import com.jy.common.StringUtil;
import com.jy.framework.base.utils.Converter;
import com.jy.rhin.application.cda.enums.HealthDictionaryEnum;
import com.jy.rhin.domain.cda.CDACreator;
import com.jy.rhin.infra.cda.CDAException;
import com.jy.rhin.infra.cda.crbbg.ChuanRanBBKVo;
import com.jy.rhin.infra.cda.feign.ShuYuService;
import com.jy.rhin.infra.cda.service.ChuanRanBBGService;
import com.jy.rhin.infra.cda.util.ZhiJiUtils;
import com.jy.rhin.model.ChuanRanBBG;
import com.jy.rhin.model.ClinicalDocumentData;
import com.jy.rhin.model.CodeObject;
import com.jy.rhin.model.body.chuanranbbg.XingZhengGL;
import com.jy.rhin.model.body.chuanranbbg.ZhenDuanJL;
import com.jy.rhin.model.header.*;
import com.jy.rhin.utils.CDAUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class ChuanRanBBGApplication {
    @Autowired
    private CDACreator cdaCreator;
    @Autowired
    private ChuanRanBBGService chuanRanBBGService;
    /*@Autowired
    private DataMigrationAPI dataMigrationAPI;*/
    @Resource
    private HospitalAPI hospitalAPI;
    @Autowired
    private ShuYuService shuYuService;

    public String create(String yiLiaoJGDM, String chuanRanBBGKBH) {
        return cdaCreator.create(createChuanRanBBG(yiLiaoJGDM, chuanRanBBGKBH), "10chuanranbbg.ftl");
    }

    public ClinicalDocumentData createChuanRanBBG(String yiLiaoJGDM, String chuanRanBBGKBH) {
        ChuanRanBBKVo chuanRanBBKVo = chuanRanBBGService.get(yiLiaoJGDM, chuanRanBBGKBH);
        if (Objects.isNull(chuanRanBBKVo)) {
            throw new CDAException("主业务数据不存在", null);
        }
        HospitalDTO hospital = hospitalAPI.info(yiLiaoJGDM).getData();
//        HospitalDTO hospital = dataMigrationAPI.getByHospitalId(yiLiaoJGDM).getData();
        if (Objects.isNull(hospital)) {
            throw new CDAException("机构数据不存在", null);
        }

        shuYuZhiYuQuery(chuanRanBBKVo);

        ChuanRanBBG docData = new ChuanRanBBG();
        setClinicalDocumentData(docData, chuanRanBBKVo, hospital);

        docData.setShouCiCXZZRQ(CDAUtil.toD8(chuanRanBBKVo.getSHOUCICXZZRQ()));

        ZhenDuanJL zhenDuanJL = new ZhenDuanJL();
        zhenDuanJL.setFaBingLB(new CodeObject(Converter.toString(chuanRanBBKVo.getCHUANRANBFBLBDM()), chuanRanBBKVo.getCHUANRANBFBLBMC()));
        zhenDuanJL.setZhenDuanZT(new CodeObject(Converter.toString(chuanRanBBKVo.getZHENDUANZTDM()), chuanRanBBKVo.getZHENDUANZTMC()));
        zhenDuanJL.setZhenDuanRQ(CDAUtil.toD8(chuanRanBBKVo.getZHENDUANRQ()));
        zhenDuanJL.setChuanRanBLB(new CodeObject(Converter.toString(chuanRanBBKVo.getCHUANRANBLBDM()), chuanRanBBKVo.getCHUANRANBLBMC()));
        zhenDuanJL.setChuanRanBMC(new CodeObject(Converter.toString(chuanRanBBKVo.getCHUANRANBMCDM()), chuanRanBBKVo.getCHUANRANBMCMC()));
        zhenDuanJL.setQiTaCRBMC(Converter.toString(chuanRanBBKVo.getQITACRBMC(), "-"));
        zhenDuanJL.setDingZhengBM(Converter.toString(chuanRanBBKVo.getDINGZHENGZD(), "-"));
        docData.setZhenDuanJL(zhenDuanJL);

        if (!"19000101".equals(Converter.toDateStr(chuanRanBBKVo.getSIWANGRQ()))) {
            docData.setSiWangRQ(CDAUtil.toD8(chuanRanBBKVo.getSIWANGRQ()));
        }

        XingZhengGL xingZhengGL = new XingZhengGL();
        xingZhengGL.setBaoKaLB(new CodeObject(Converter.toString(chuanRanBBKVo.getBAOKALBDM()), chuanRanBBKVo.getBAOKALBMC()));
        if (chuanRanBBKVo.getCHUANRANBHZGSDM() == null) {
            chuanRanBBKVo.setCHUANRANBHZGSDM(1);
        }
        xingZhengGL.setHuanZheGS(new CodeObject(Converter.toString(chuanRanBBKVo.getCHUANRANBHZGSDM()), chuanRanBBKVo.getCHUANRANBHZGSMC()));
        xingZhengGL.setTuiKaYY(Converter.toString(chuanRanBBKVo.getTUIKAYUANYIN(), "-"));
        docData.setXingZhengGL(xingZhengGL);

        setDefaultDocData(docData);
        return docData;
    }

    private void shuYuZhiYuQuery(ChuanRanBBKVo chuanRanBBKVo) {
        List<ZhiYuMXCXDto> zhiJiBSFList = new ArrayList<>();
        ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CVX_CRBFBLB.getCode(), new HashSet<String>(Arrays.asList(Converter.toString(chuanRanBBKVo.getCHUANRANBFBLBDM()))));
        ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CV0_01_002.getCode(), new HashSet<String>(Arrays.asList(Converter.toString(chuanRanBBKVo.getZHENDUANZTDM()))));
        ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CVX_CRBLB.getCode(), new HashSet<String>(Arrays.asList(Converter.toString(chuanRanBBKVo.getCHUANRANBLBDM()))));
        ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CV05_01_017.getCode(), new HashSet<String>(Arrays.asList(Converter.toString(chuanRanBBKVo.getCHUANRANBMCDM()))));
        ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CVX_BAOKALB.getCode(), new HashSet<String>(Arrays.asList(Converter.toString(chuanRanBBKVo.getBAOKALBDM()))));
        ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CV02_01_104.getCode(), new HashSet<String>(Arrays.asList(Converter.toString(chuanRanBBKVo.getCHUANRANBHZGSDM()))));

        Map<String, List<DaiMaDto>> valueByCode = shuYuService.getValueByCode(zhiJiBSFList.toArray(new ZhiYuMXCXDto[0]));

        chuanRanBBKVo.setCHUANRANBFBLBMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CVX_CRBFBLB.getCode(), Converter.toString(chuanRanBBKVo.getCHUANRANBFBLBDM()), valueByCode));
        chuanRanBBKVo.setZHENDUANZTMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CV0_01_002.getCode(), Converter.toString(chuanRanBBKVo.getZHENDUANZTDM()), valueByCode));
        chuanRanBBKVo.setCHUANRANBLBMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CVX_CRBLB.getCode(), Converter.toString(chuanRanBBKVo.getCHUANRANBLBDM()), valueByCode));
        chuanRanBBKVo.setCHUANRANBMCMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CV05_01_017.getCode(), Converter.toString(chuanRanBBKVo.getCHUANRANBMCDM()), valueByCode));
        chuanRanBBKVo.setBAOKALBMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CVX_BAOKALB.getCode(), Converter.toString(chuanRanBBKVo.getBAOKALBDM()), valueByCode));
        chuanRanBBKVo.setCHUANRANBHZGSMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CV02_01_104.getCode(), Converter.toString(chuanRanBBKVo.getCHUANRANBHZGSDM()), valueByCode));
    }

    ClinicalDocumentData setClinicalDocumentData(ClinicalDocumentData clinicalDocumentData, ChuanRanBBKVo chuanRanBBKVo, HospitalDTO hospital) {
        clinicalDocumentData.setEffectiveTime(CDAUtil.toD15(chuanRanBBKVo.getKAPIANCJSJ()));
        clinicalDocumentData.setPatientRole(createPatientRole(chuanRanBBKVo));
        clinicalDocumentData.setAuthor(createAuthor(chuanRanBBKVo, hospital));
        clinicalDocumentData.setCustodian(createCustodian(hospital));
        clinicalDocumentData.setId(String.valueOf(chuanRanBBKVo.getId()));
        return clinicalDocumentData;
    }

    Author createAuthor(ChuanRanBBKVo chuanRanBBKVo, HospitalDTO hospital) {
        Author author = new Author();
        author.setTime(CDAUtil.toD15(chuanRanBBKVo.getBAOGAORQ()));
        author.setId(chuanRanBBKVo.getTIANKAYSXM());
        author.setName(chuanRanBBKVo.getTIANKAYSXM());
        author.setOrganizationId(CDAUtil.toJGID10(hospital.getOutId()));
        author.setOrganizationName(hospital.getName());
        author.setOrganizationTelecom(hospital.getDanWeiDH());
        if (StringUtil.isNullOrEmpty(author.getOrganizationTelecom())) {
            author.setOrganizationTelecom("010-99999999");
        }
        author.setOrganizationAddr(hospital.getAddress());
        return author;
    }

    Custodian createCustodian(HospitalDTO hospital) {
        Custodian custodian = new Custodian();
        custodian.setId(CDAUtil.toJGID10(hospital.getOutId()));
        custodian.setName(hospital.getName());
        custodian.setTelecom(hospital.getDanWeiDH());
        custodian.setAddr(hospital.getAddress());
        return custodian;
    }

    PatientRole createPatientRole(ChuanRanBBKVo chuanRanBBKVo) {
        PatientRole patientRole = new PatientRole();
        patientRole.setId(CDAUtil.formatNumber(chuanRanBBKVo.getGERENDABS()));
        patientRole.setTelecom(chuanRanBBKVo.getLIANXIDH());
        patientRole.setAddr(createAddr(chuanRanBBKVo));
        patientRole.setPatient(createPatient(chuanRanBBKVo));
        return patientRole;
    }

    Addr createAddr(ChuanRanBBKVo chuanRanBBKVo) {
        Addr addr = new Addr();
        addr.setState(chuanRanBBKVo.getXIANZHUDZMC());
        addr.setCity(chuanRanBBKVo.getXIANZHUDZMC());
        addr.setCounty(chuanRanBBKVo.getXIANZHUDZMC());
        addr.setTownship(chuanRanBBKVo.getXIANZHUDZMC());
        addr.setStreetName(chuanRanBBKVo.getXIANZHUDZMC());
        addr.setHouseNumber(chuanRanBBKVo.getXIANGXIDZ());
        return addr;
    }

    Patient createPatient(ChuanRanBBKVo chuanRanBBKVo) {
        Patient patient = new Patient();
        patient.setId(chuanRanBBKVo.getZHENGJIANHM());
        patient.setName(chuanRanBBKVo.getHUANZHEXM());
        patient.setAdministrativeGender(new CodeObject(Converter.toString(chuanRanBBKVo.getXINGBIEDM()), chuanRanBBKVo.getXINGBIEMC()));
        patient.setBirthTime(CDAUtil.toD8(chuanRanBBKVo.getCHUSHENGRQ()));
        patient.setGuardian(createGuardian(chuanRanBBKVo));
        patient.setEmployerOrganization(chuanRanBBKVo.getGONGZUODANWEI());
        patient.setTelecom("-");
        patient.setHouseholdAddr(createHouseholdAddr(chuanRanBBKVo));
        patient.setEmployerOrganizationAddr(createEmployerAddr(chuanRanBBKVo));
        patient.setOccupation(new CodeObject("09", "工人"));
        return patient;
    }

    private Addr createEmployerAddr(ChuanRanBBKVo chuanRanBBKVo) {
        Addr addr = new Addr();
        addr.setState("广东省");
        addr.setCity("深圳市");
        addr.setCounty("罗湖");
        addr.setTownship("桂园街道");
        addr.setStreetName("红桂路");
        addr.setHouseNumber("2088号");
        return addr;
    }

    Guardian createGuardian(ChuanRanBBKVo chuanRanBBKVo) {
        Guardian guardian = new Guardian();
        guardian.setName(chuanRanBBKVo.getJIASHUXM());
        guardian.setTelecom(chuanRanBBKVo.getLIANXIDH());
        guardian.setCode(new CodeObject("97", "其他亲属"));
        return guardian;
    }

    Addr createHouseholdAddr(ChuanRanBBKVo chuanRanBBKVo) {
        Addr addr = new Addr();
        addr.setState(chuanRanBBKVo.getHUJIDZMC());
        addr.setCity(chuanRanBBKVo.getHUJIDZMC());
        addr.setCounty(chuanRanBBKVo.getHUJIDZMC());
        addr.setTownship(chuanRanBBKVo.getHUJIDZMC());
        addr.setStreetName(chuanRanBBKVo.getHUJIDZMC());
        addr.setHouseNumber(chuanRanBBKVo.getHUJIXXDZ());
        return addr;
    }

    private void setDefaultDocData(ChuanRanBBG docdata) {
        if (StringUtil.isNullOrEmpty(docdata.getShouCiCXZZRQ())) {
            docdata.setShouCiCXZZRQ(CDAUtil.defaultD8());
        }
        if (docdata.getZhenDuanJL() == null) {
            ZhenDuanJL zhenDuanJL = new ZhenDuanJL();
            zhenDuanJL.setFaBingLB(new CodeObject("2", "慢性"));
            zhenDuanJL.setZhenDuanZT(new CodeObject("4", "病原携带者"));
            zhenDuanJL.setZhenDuanRQ(CDAUtil.defaultD8());
            zhenDuanJL.setChuanRanBLB(new CodeObject("3", "丙类"));
            zhenDuanJL.setChuanRanBMC(new CodeObject("2", "乙类传染病"));
            zhenDuanJL.setQiTaCRBMC("-");
            zhenDuanJL.setDingZhengBM("-");
            docdata.setZhenDuanJL(zhenDuanJL);
        }
        if (StringUtil.isNullOrEmpty(docdata.getSiWangRQ())) {
            docdata.setSiWangRQ(CDAUtil.defaultD8());
        }
        if (docdata.getXingZhengGL() == null) {
            XingZhengGL xingZhengGL = new XingZhengGL();
            xingZhengGL.setBaoKaLB(new CodeObject("1","初次报告"));
            xingZhengGL.setHuanZheGS(new CodeObject("1","本县区"));
            xingZhengGL.setTuiKaYY("无");
            docdata.setXingZhengGL(xingZhengGL);
        }
    }
}
